<?php
/** 
 * Add the water mark by using a picture
 * Cavin Feb 11,2012
 */
class addWaterMark{
	
	public $maxPicWidth = '650';
	public $waterMarkPicPath = '../../../../../img/logo_wm.gif';
	private $origPicPath = '';
	private $picAlpha = '30';
	private $picFileName;
	private $picFilePath;
	private $picFileType;
	private $imageTypeArray = array("1"=>"gif","2"=>"jepg","3"=>"png");  
	
	public function addWaterMark(array $picInfoArr, $picAlpha = '') {
		
		$this->picFileName = $picInfoArr['name'];
		$this->picFilePath = $picInfoArr['path'];
		$this->picFileType = $picInfoArr['ext'];
		$this->origPicPath = $picInfoArr['path'].'/'.$picInfoArr['name'];
		if($picAlpha != '')
		{
			$this -> picAlpha = $picAlpha;
		}
	}
	
	public function appendPicWaterMark()
	{
		if(file_exists($this->origPicPath) && is_file($this->origPicPath)) 
		{
			$SrcImageType = getimagesize($this->origPicPath);	
		}
		else {
			errorEventLog::displaysSQLPhase($this->origPicPath . ' file does not exist.');
            return false;
        }
		$SrcImageTypeW = $SrcImageType[0]; //get the pic width
        $SrcImageTypeH = $SrcImageType[1]; //get the pic height
        
        //check the water marked picture 
		if(file_exists($this->waterMarkPicPath)) 
		{
			$SrcMarkImageType = getimagesize($this->waterMarkPicPath);	
		}
		else {
			errorEventLog::displaysSQLPhase(file_exists($this->waterMarkPicPath));
			errorEventLog::displaysSQLPhase($this->waterMarkPicPath . ' file does not exist.');
            return false;
        }
        //errorEventLog::displaysSQLPhase('width:'.$SrcMarkImageType[0].'; height:'.$SrcMarkImageType[1]);
        $SrcMarkImageTypeW = $SrcMarkImageType[0];
        $SrcMarkImageTypeH = $SrcMarkImageType[1];
        
        $imgMarkObj = self::createImageFromRespectiveType($this->imageTypeArray[$SrcMarkImageType[2]],$this->waterMarkPicPath);
        //errorEventLog::displaysSQLPhase('image type:'.$this->imageTypeArray[$SrcMarkImageType[2]]);
        //check if we need to resize the uploaded picture as there is an limitation for width
        if($SrcImageTypeW > $this->maxPicWidth)
        {
        	//errorEventLog::displaysSQLPhase($this->picFileType,$this->picFilePath);
        	$img = self::createImageFromRespectiveType($this->picFileType,$this->origPicPath);
        	//if the width is exceeded the width allowed ,then resize it	
        	$newWidth = $this->maxPicWidth;
        	$newHeight = $SrcImageTypeH * ($newWidth/$SrcImageTypeW);
        	
        	$ReIm = imagecreatetruecolor($newWidth, $newHeight);
        	
        	 //  imagecopyresampled($resizedImage, $original, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);
            @imagecopyresampled($ReIm, $img, 0, 0, 0, 0, $newWidth,$newHeight, $SrcImageTypeW, $SrcImageTypeH);
            
            $SrcImageTypeW = $this->maxPicWidth;
            $SrcImageTypeH = $newHeight;
            
            if ($this->picFileType == "gif")
            {
                        imagegif($ReIm, $this->origPicPath);
            }
            elseif ($this->picFileType == "jpg")
            {
                    	imagejpeg($ReIm, $this->origPicPath);
            }      
            elseif ($this->picFileType == "png")
            {
                    	imagepng($ReIm, $this->origPicPath);
            }    
        }
        $SrcImageType = self::createImageFromRespectiveType($this->picFileType,$this->origPicPath);
        
      	if($SrcImageTypeW > $SrcMarkImageTypeW && $SrcImageTypeH > $SrcMarkImageTypeH)
        {
        	$picMarkPosition = rand(1,4);
        	
        	switch ($picMarkPosition) {
        		case '1'://lb
        			$picMarkX = ($SrcImageTypeW-$SrcMarkImageTypeW-10);
        			$picMarkY = 10;
        		break;
        		case '2'://lt
        			$picMarkX = 10;
        			$picMarkY = 10;
        		break;
        		case '3'://rt
        			$picMarkX = 10;
        			$picMarkY = ($SrcImageTypeH-$SrcMarkImageTypeH-10);
        		break;
        		default: //rb
        			$picMarkX = ($SrcImageTypeW-$SrcMarkImageTypeW-10);
        			$picMarkY = ($SrcImageTypeH-$SrcMarkImageTypeH-10);
        		break;
        	}
	        //append the waterMark
	        
	        $rs = imagecopymerge($SrcImageType,$imgMarkObj,$picMarkX,$picMarkY,0,0,$SrcMarkImageTypeW,$SrcMarkImageTypeH,$this->picAlpha);
			//if($rs)
			//{
			//	errorEventLog::displaysSQLPhase('Picture Merged Successfully!');
			//}
			//else
			//{
			//	errorEventLog::displaysSQLPhase($SrcImageTypeW-$SrcMarkImageTypeW.','.   
			//					$SrcImageTypeH-$SrcMarkImageTypeH.','.$SrcMarkImageTypeW.','.     
			//						$SrcMarkImageTypeH.','.$this->picAlpha);
			//}
			
			if ($this->picFileType == "gif")
	        {
	                        imagegif($SrcImageType, $this->origPicPath);
	        }
	        elseif ($this->picFileType == "jpg")
	        {
	                    	imagejpeg($SrcImageType, $this->origPicPath);
	        }      
	        elseif ($this->picFileType == "png")
	        {
	                    	imagepng($SrcImageType, $this->origPicPath);
	        }
        }
		return true;
	}
	
	public function createImageFromRespectiveType($imgType,$imgPath)
	{
		switch($imgType)
		{
			case "jpg":
				$img = imagecreatefromjpeg($imgPath);
				break;
			case "gif":
				$img = imagecreatefromgif($imgPath);
				break;
			case "png":
				$img = imagecreatefrompng($imgPath);
				break;
		}
		return $img;
		
	}
	

}

?>